package net.seehope.foodie.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import net.seehope.foodie.exception.PassportException;
import net.seehope.foodie.mapper.UsersMapper;
import net.seehope.foodie.pojo.Users;

@Service
public class UserDetailServiceImpl implements UserDetailsService{
	
	@Autowired
	private UsersMapper userMapper;
	
	//条件装配
	//username是从token传进来的
	//用户名跟数据库匹配上了
	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		Users users = new Users();
		users.setUsername(username);
		Users one = userMapper.selectOne(users);
		
		
		if(one==null) {
			throw new PassportException("用户名或者密码不匹配");
		}
		
		User user = new User(one.getUsername(), one.getPassword(), true, true, true, true,
				AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_USER,ROLE_ADMIN"));
		System.out.println(username);
		return user;
	}
	
	
	

}
